package l1j.server.server.model.clan;

import java.sql.Timestamp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.Config;
import l1j.server.L1DatabaseFactory;
import l1j.server.server.datatables.ClanTable;
import l1j.server.server.model.L1Clan;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.utils.SQLUtil;
import l1j.server.server.utils.TimeUtil;

public class ClanHouse {
	private static Logger _log = Logger.getLogger(ClanHouse.class.getName());
	private int _houseId;
	private String _houseName;
	private int _houseArea;
	private Timestamp _deadLine;
	private int _price;
	private String _location;
	private String _owner;
	private int _ownerId;
	private int _keeperId;

	private ClanHouse() {

	}

	public static ClanHouse getHouseById(int houseId) {
		ClanHouse house = null;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;

		try {
			con = L1DatabaseFactory.getInstance().getConnection();

			pstm = con
					.prepareStatement("SELECT * FROM board_auction where house_id = ?");
			pstm.setInt(1, houseId);
			rs = pstm.executeQuery();
			if (rs.next()) {
				house = new ClanHouse();
				house.setHouseId(rs.getInt("house_id"));
				house.setHouseName(rs.getString("house_name"));
				house.setHouseArea(rs.getInt("house_area"));
				house.setDeadLine(rs.getTimestamp("deadline"));
				house.setPrice(rs.getInt("price"));
				house.setLocation(rs.getString("location"));
				house.setOwner(rs.getString("owner"));
				house.setOwnerId(rs.getInt("owner_id"));
				house.setKeeperId(rs.getInt("keeper_id"));
			}
		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		return house;
	}

	public static ClanHouse getHouseByKeeper(int keeperId) {
		ClanHouse house = null;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;

		try {
			con = L1DatabaseFactory.getInstance().getConnection();

			pstm = con
					.prepareStatement("SELECT * FROM board_auction where keeper_id = ?");
			pstm.setInt(1, keeperId);
			rs = pstm.executeQuery();
			if (rs.next()) {
				house = new ClanHouse();
				house.setHouseId(rs.getInt("house_id"));
				house.setHouseName(rs.getString("house_name"));
				house.setHouseArea(rs.getInt("house_area"));
				house.setDeadLine(rs.getTimestamp("deadline"));
				house.setPrice(rs.getInt("price"));
				house.setLocation(rs.getString("location"));
				house.setOwner(rs.getString("owner"));
				house.setOwnerId(rs.getInt("owner_id"));
				house.setKeeperId(rs.getInt("keeper_id"));
			}
		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		return house;
	}

	public int getHouseId() {
		return _houseId;
	}

	public void setHouseId(int _houseId) {
		this._houseId = _houseId;
	}

	public String getHouseName() {
		return _houseName;
	}

	public void setHouseName(String _houseName) {
		this._houseName = _houseName;
	}

	public int getHouseArea() {
		return _houseArea;
	}

	public void setHouseArea(int _houseArea) {
		this._houseArea = _houseArea;
	}

	public Timestamp getDeadLine() {
		return _deadLine;
	}

	public void setDeadLine(Timestamp _deadLine) {
		this._deadLine = _deadLine;
	}

	public int getPrice() {
		return _price;
	}

	public void setPrice(int _price) {
		this._price = _price;
	}

	public String getLocation() {
		return _location;
	}

	public void setLocation(String _location) {
		this._location = _location;
	}

	public String getOwner() {
		return _owner;
	}

	public void setOwner(String _owner) {
		this._owner = _owner;
	}

	public int getOwnerId() {
		return _ownerId;
	}

	public void setOwnerId(int _ownerId) {
		this._ownerId = _ownerId;
	}

	public int getKeeperId() {
		return _keeperId;
	}

	public void setKeeperId(int _keeperId) {
		this._keeperId = _keeperId;
	}

	public void buyHouse(L1PcInstance pc) {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();

			pstm = con
					.prepareStatement("update board_auction set owner_id = ?,owner = ?,deadline = ? where house_id = ?");
			pstm.setInt(1, pc.getId());
			pstm.setString(2, pc.getName());

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			Calendar cal = TimeUtil.getCalendar();
			cal.add(Calendar.DATE, Config.HOUSE_TAX_INTERVAL);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			String fm = sdf.format(cal.getTime());

			pstm.setString(3, fm);
			pstm.setInt(4, _houseId);
			pstm.execute();
		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		pc.sendPackets(new S_ServerMessage(524, String.valueOf(_price), pc
				.getName()));
		pc.getClan().setHouseId(_houseId);
		ClanTable.getInstance().updateClan(pc.getClan());
	}

	public void sellHouse() {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();

			pstm = con
					.prepareStatement("update board_auction set owner_id = ?,owner = ?,deadline = ?,house_name = ? where house_id = ?");
			pstm.setInt(1, 0);
			pstm.setString(2, "");

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			Calendar cal = TimeUtil.getCalendar();
			cal.add(Calendar.DATE, Config.HOUSE_TAX_INTERVAL);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			String fm = sdf.format(cal.getTime());

			pstm.setString(3, fm);
			pstm.setString(4, _location);
			pstm.setInt(5, _houseId);
			pstm.execute();
		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		for (L1Clan clan : L1World.getInstance().getAllClans()) {
			if (clan.getHouseId() == _houseId) {
				clan.setHouseId(0);
				ClanTable.getInstance().updateClan(clan);
			}
		}
	}

	public void changeHouseName(String name) {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();

			pstm = con
					.prepareStatement("update board_auction set house_name = ? where house_id = ?");
			pstm.setString(1, name);
			pstm.setInt(2, _houseId);
			pstm.execute();
		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}

	public void extendDeadline() {
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();

			pstm = con
					.prepareStatement("update board_auction set deadline = ? where house_id = ?");
			Calendar cal = TimeUtil.timestampToCalendar(_deadLine);
			cal.add(Calendar.DATE, Config.HOUSE_TAX_INTERVAL);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			String fm = sdf.format(cal.getTime());

			pstm.setString(1, fm);
			pstm.setInt(2, _houseId);
			pstm.execute();
		} catch (SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
}
